package com.cositas.dulces.lp.promo2012;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cositas.dulces.lp.promo2012.dbentities.Recommendation;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Query;

public class DownloadRecommendationsAction implements Action{
	
	public void doAction(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		BackendAction.checkUser();
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Query query = new Query(Recommendation.entity_name);
		List<Entity> greetings = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(10000));
		resp.getWriter().print("key,");
		resp.getWriter().print(Recommendation.field_fromfbid);
		resp.getWriter().print(",");
		resp.getWriter().println(Recommendation.field_tofbid);
		for (Entity e : greetings) {
			resp.getWriter().print(e.getKey());
			resp.getWriter().print(",");
			resp.getWriter().print(e.getProperty(Recommendation.field_fromfbid));
			resp.getWriter().print(",");
			resp.getWriter().println(e.getProperty(Recommendation.field_tofbid));
		}
	}
}
